import os


class EmptyDirCleaner:
    def __init__(self, target_directory):
        self.target_directory = target_directory

    @staticmethod
    def is_empty(directory):
        # Check if the directory is empty and doesn't contain hidden files
        for root, dirs, files in os.walk(directory):
            if dirs or files:
                for name in files + dirs:
                    if not name.startswith('.'):
                        return False
        return True

    def find_empty_dirs(self):
        empty_dirs = []
        # Walk through the directory
        for root, dirs, files in os.walk(self.target_directory, topdown=False):
            for dir in dirs:
                dir_path = os.path.join(root, dir)
                if self.is_empty(dir_path):
                    empty_dirs.append(dir_path)
        return empty_dirs

    @staticmethod
    def clean_empty_dirs(empty_dirs):
        for dir_path in empty_dirs:
            os.rmdir(dir_path)
            print(f"Deleted empty directory: {dir_path}")

    def run(self):
        empty_dirs = self.find_empty_dirs()
        if empty_dirs:
            print("Found the following empty directories:")
            for dir in empty_dirs:
                print(dir)
            confirm = input("Do you want to delete these directories? (yes/no): ").strip().lower()
            if confirm == 'yes':
                self.clean_empty_dirs(empty_dirs)
            else:
                print("No directories were deleted.")
        else:
            print("No empty directories found.")

#
# class EmptyDirCleaner:
#     def __init__(self, target_directory):
#         self.target_directory = target_directory
#
#     @staticmethod
#     def is_empty(directory):
#         # Check if the directory is empty and doesn't contain hidden files
#         for root, dirs, files in os.walk(directory):
#             if dirs or files:
#                 for name in files + dirs:
#                     if not name.startswith('.'):
#                         return False
#         return True
#
#     def clean_empty_dirs(self):
#         # Walk through the directory
#         for root, dirs, files in os.walk(self.target_directory, topdown=False):
#             for dir in dirs:
#                 dir_path = os.path.join(root, dir)
#                 if self.is_empty(dir_path):
#                     # os.rmdir(dir_path)
#                     print(f"Deleted empty directory: {dir_path}")
#                 else:
#                     print(f"Did not delete {dir_path}: Directory is not empty")
